﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_Accounts_AccountBooks_CashBook : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    string finyear = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
        }
    }

    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                      new SqlParameter("@ISFIRSTYEAR",_IsFirstYear(Session["FINYEAR"].ToString())),
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString())
                                  };

            dtMain = DU.getdatausingprocedure("USP_ACC_GetCashLedgerBalance", sqlparam);
        }
        lblMsg.Text = "Cash Book : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        grdCashBook.DataSource = dtMain;
        grdCashBook.DataBind();
    }

    protected void grdCashBook_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Attributes.Add("rowspan", "2");
            e.Row.Cells[1].Attributes.Add("rowspan", "2");
            e.Row.Cells[2].Attributes.Add("colspan", "2");
            e.Row.Cells[2].Text = "Transactions";
            e.Row.Cells.RemoveAt(3);
            string strNewRow = "</td></tr><tr style=\"text-align:center; font-weight:bold;\"><td>Debit</td><td>Credit";
            e.Row.Cells[3].Text = e.Row.Cells[3].Text + strNewRow;
            e.Row.Cells[3].Attributes.Add("rowspan", "2");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string[] open = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "OpeningBAL")).Split(' ');
            string[] close = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ClosingBAL")).Split(' ');
            HyperLink hyperLedger = (HyperLink)e.Row.FindControl("hyperLedger");
            HiddenField hidLID = (HiddenField)e.Row.FindControl("hidLID");

            hyperLedger.Attributes.Add("onclick", "return OpenMonthlySummary('" + hidLID.Value.ToString() + "','" + hyperLedger.Text.ToString() + "','" + txtFromDate.Text + "','" + txtToDate.Text + "')");
            //Response.Write(hyperLedger.Text);
            Double openBAL = 0;
            Double closeBAL = 0;
            
            if (open[0].ToString() != string.Empty)
            {
                 openBAL = Convert.ToDouble(open[0].ToString());
            }
            if (close[0].ToString() != string.Empty)
            {
                 closeBAL = Convert.ToDouble(close[0].ToString());
            }
            Double tranDr = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "DebitBAL"));
            Double tranCr = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "CreditBAL"));
            if (openBAL > 0)
            {
                e.Row.Cells[1].Text = acntstng.GetCurrency(openBAL) + " " + open[1].ToString();
            }
            if (closeBAL > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(closeBAL) + " " + close[1].ToString();
            }
            if (tranDr > 0)
            {
                e.Row.Cells[2].Text = acntstng.GetCurrency(tranDr);
            }
            if (tranCr > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(tranCr);
            }
        }
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
       BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdCashBook_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         grdCashBook.PageIndex = e.NewPageIndex;
         BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    private string _IsFirstYear(string strFinYear)
    {
        string strIsFirstYear = string.Empty;
        string strSql = "SELECT MIN(FINYEAR)AS FINYEAR FROM MAS_FINYEAR";
        DataTable DTMinFinyear = DU.getdata(strSql);
        if (DTMinFinyear.Rows.Count > 0)
        {
            if (Convert.ToInt32(DTMinFinyear.Rows[0]["FINYEAR"].ToString()) == Convert.ToInt32(strFinYear))
            {
                strIsFirstYear = "1";
            }
            else
            {
                strIsFirstYear = "0";
            }
        }
        return strIsFirstYear;
    }

}
